package hedron.editor;

import hedron.Activator;
import hedron.Application;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.DetailsPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.MasterDetailsBlock;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

public class __DemoEditorPaged extends FormEditor {
	public static final String ID = "HEDRON.DemoPagedFormEditor";

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		setSite(site);
		setInput(input);
	}

	public void dispose() {
		super.dispose();
	}


	@Override
	protected void addPages() {
		try {
			addPage(new FirstPage(this));
			addPage(new SecondPage(this));
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}


	@Override
	public void setFocus() {
	}

	@Override
	public void doSave(IProgressMonitor monitor) {}

	@Override
	public void doSaveAs() {}



	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	public static class FirstPage extends FormPage {

		public FirstPage(FormEditor editor) {
			super(editor, "first", "First Page");
		}

		protected void createFormContent(IManagedForm managedForm) {
			ScrolledForm form = managedForm.getForm();
			FormToolkit toolkit = managedForm.getToolkit();
			form.setText("title");
			// form.setBackgroundImage(FormArticlePlugin.getDefault().getImage(FormArticlePlugin.IMG_FORM_BG));
			TableWrapLayout layout = new TableWrapLayout();
			layout.leftMargin = 10;
			layout.rightMargin = 10;
			form.getBody().setLayout(layout);
			TableWrapData td;
			Hyperlink link = toolkit.createHyperlink(form.getBody(), "Link", SWT.WRAP); //$NON-NLS-1$
			link.addHyperlinkListener(new HyperlinkAdapter() {
				public void linkActivated(HyperlinkEvent e) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException ex) {}
				}
			});
			td = new TableWrapData();
			td.align = TableWrapData.LEFT;
			link.setLayoutData(td);
			createExpandable(form, toolkit);
			createFormTextSection(form, toolkit);
		}

		private void createExpandable(final ScrolledForm form, final FormToolkit toolkit) {
			final ExpandableComposite exp = toolkit.createExpandableComposite(form.getBody(),
					ExpandableComposite.TREE_NODE
			// ExpandableComposite.NONE
			);
			exp.setActiveToggleColor(toolkit.getHyperlinkGroup().getActiveForeground());
			Composite client = toolkit.createComposite(exp);
			exp.setClient(client);
			TableWrapLayout elayout = new TableWrapLayout();
			client.setLayout(elayout);
			elayout.leftMargin = elayout.rightMargin = 0;
			final Button button = toolkit.createButton(client, "button", SWT.PUSH); //$NON-NLS-1$
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
				// openFormWizard(button.getShell(), toolkit.getColors());
				}
			});
			exp.addExpansionListener(new ExpansionAdapter() {
				public void expansionStateChanged(ExpansionEvent e) {
					form.reflow(true);
				}
			});
			exp.setText("title 3"); //$NON-NLS-1$
			TableWrapData td = new TableWrapData();
			// td.colspan = 2;
			td.align = TableWrapData.LEFT;
			// td.align = TableWrapData.FILL;
			exp.setLayoutData(td);
		}

		private void createFormTextSection(final ScrolledForm form, FormToolkit toolkit) {
			Section section = toolkit.createSection(form.getBody(), Section.TWISTIE | Section.DESCRIPTION);
			section.setActiveToggleColor(toolkit.getHyperlinkGroup().getActiveForeground());
			// section.setToggleColor(toolkit.getColors().getColor(FormColors.SEPARATOR));
			toolkit.createCompositeSeparator(section);
			FormText rtext = toolkit.createFormText(section, false);
			section.setClient(rtext);

			section.addExpansionListener(new ExpansionAdapter() {
				public void expansionStateChanged(ExpansionEvent e) {
					form.reflow(false);
				}
			});

			section.setText("title 2"); //$NON-NLS-1$
			section.setDescription("description"); //$NON-NLS-1$
			TableWrapData td = new TableWrapData();
			td.align = TableWrapData.FILL;
			td.grabHorizontal = true;
			section.setLayoutData(td);
		}

	}

	public static class SecondPage extends FormPage {
		private ScrolledPropertiesBlock block;

		public SecondPage(FormEditor editor) {
			super(editor, "fourth", "MasterDetailsPage.label");
			block = new ScrolledPropertiesBlock(this);
		}

		protected void createFormContent(final IManagedForm managedForm) {
			final ScrolledForm form = managedForm.getForm();
			// FormToolkit toolkit = managedForm.getToolkit();
			form.setText("MasterDetailsPage.title");
			form.setBackgroundImage(Activator.getImage("icons/form_background.png"));
			block.createContent(managedForm);
		}
	}

	public static class ScrolledPropertiesBlock extends MasterDetailsBlock {
		private FormPage page;

		public ScrolledPropertiesBlock(FormPage page) {
			this.page = page;
		}

		/**
		 * @param id
		 * @param title
		 */
		class MasterContentProvider implements IStructuredContentProvider {
			public Object[] getElements(Object inputElement) {
				return new Object[0];
			}

			public void dispose() {}

			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
		}

		protected void createMasterPart(final IManagedForm managedForm, Composite parent) {
			// final ScrolledForm form = managedForm.getForm();
			FormToolkit toolkit = managedForm.getToolkit();
			Section section = toolkit.createSection(parent, Section.DESCRIPTION | Section.TITLE_BAR);
			section.setText("ScrolledPropertiesBlock.sname"); //$NON-NLS-1$
			section.setDescription("ScrolledPropertiesBlock.sdesc"); //$NON-NLS-1$
			section.marginWidth = 10;
			section.marginHeight = 5;
			Composite client = toolkit.createComposite(section, SWT.WRAP);
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			layout.marginWidth = 2;
			layout.marginHeight = 2;
			client.setLayout(layout);
			Table t = toolkit.createTable(client, SWT.NULL);
			GridData gd = new GridData(GridData.FILL_BOTH);
			gd.heightHint = 20;
			gd.widthHint = 100;
			t.setLayoutData(gd);
			toolkit.paintBordersFor(client);
			Button b = toolkit.createButton(client, "ScrolledPropertiesBlock.add", SWT.PUSH); //$NON-NLS-1$
			gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
			b.setLayoutData(gd);
			section.setClient(client);
			final SectionPart spart = new SectionPart(section);
			managedForm.addPart(spart);
			TableViewer viewer = new TableViewer(t);
			viewer.addSelectionChangedListener(new ISelectionChangedListener() {
				public void selectionChanged(SelectionChangedEvent event) {
					managedForm.fireSelectionChanged(spart, event.getSelection());
				}
			});
			viewer.setContentProvider(new MasterContentProvider());
			viewer.setInput(page.getEditor().getEditorInput());
		}

		protected void createToolBarActions(IManagedForm managedForm) {
			final ScrolledForm form = managedForm.getForm();

			Action haction = new Action("hor", Action.AS_PUSH_BUTTON) { //$NON-NLS-1$
				public void run() {
					Application.setStatusMessage(0, "horizontal");
				}
			};
			haction.setToolTipText("horizontal");
			haction.setImageDescriptor(Activator.getImageDescriptor("icons/dir.png"));

			Action vaction = new Action("ver", Action.AS_PUSH_BUTTON) { //$NON-NLS-1$
				public void run() {
					Application.setStatusMessage(0, "vertical");
				}
			};
			vaction.setToolTipText("vertical");
			vaction.setImageDescriptor(Activator.getImageDescriptor("icons/file.png"));

			form.getToolBarManager().add(haction);
			form.getToolBarManager().add(vaction);
		}

		@Override
		protected void registerPages(DetailsPart detailsPart) {}
	}

}
